package com.jsyh.maidu.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.jsyh.maidu.R;
import com.jsyh.maidu.adapter.ArticleListAdapter;
import com.jsyh.maidu.model.ArticleListChild;
import com.jsyh.maidu.model.ArticleListModel;
import com.jsyh.maidu.model.ArticleModel;
import com.jsyh.maidu.presenter.ArticlePresenter;
import com.jsyh.maidu.views.ArticleView;
import com.jsyh.shopping.uilibrary.divider.DividerItemDecoration;

import java.util.List;

/**
 * Created by ZHY_9 on 2017/7/12.
 */

public class ArticleListActivity extends BaseActivity implements View.OnClickListener, ArticleView {


    private RecyclerView articleList;
    private EditText keywordEd;
    private ImageView search;

    private TextView title;
    private FrameLayout back;

    private ArticlePresenter presenter;

    private ArticleListAdapter adapter;
    private List<ArticleListChild> datas;
    private LinearLayoutManager layoutManager;

    private String cat_id;
    private int page;
    private String keyword;

    @Override
    public void initView() {
        super.initView();
        setContentView(R.layout.activity_article_list);
        articleList = (RecyclerView) findViewById(R.id.article_child_list);
        keywordEd = (EditText) findViewById(R.id.article_keyword);
        search = (ImageView) findViewById(R.id.search_img);
        layoutManager = new LinearLayoutManager(this);
        articleList.setLayoutManager(layoutManager);
        articleList.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        search.setOnClickListener(this);
    }

    @Override
    public void initData() {
        super.initData();
        Intent intent = getIntent();
        cat_id = intent.getStringExtra("cat_id");
        presenter = new ArticlePresenter(this);
        page = 1;
        if (TextUtils.isEmpty(keyword)) {
            keyword = "";
        }
        presenter.getArticleList(this, cat_id, String.valueOf(page), keyword);

    }

    @Override
    public void initTitle() {
        super.initTitle();
        title = (TextView) findViewById(R.id.title);
        back = (FrameLayout) findViewById(R.id.fl_Left);
        title.setText("文章列表");
        back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.fl_Left:
                finish();
                break;
            case R.id.search_img:
                keyword = keywordEd.getText().toString().trim();
                presenter.getArticleList(this, cat_id, String.valueOf(page), keyword);
                break;
        }
    }

    @Override
    public void getArticles(ArticleModel model) {

    }

    @Override
    public void getArticleList(ArticleListModel model) {
        if (model.getCode().equals("1")) {
            datas = model.getData().getArticles();
            adapter = new ArticleListAdapter(datas, this);
            articleList.setAdapter(adapter);
        }
    }
}
